package com.zxx.pattern.commandPattern.command3;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.zxx.designpattern.R;

public class DrawActivity extends AppCompatActivity implements View.OnClickListener {

    private DrawCanvas mCanvas;     //绘制画布
    private IDrawPath mPath;        //路径绘制命令
    private Paint mPaint;           //画笔对象
    private IBrush mBrush;          //笔触对象

    private Button redBtn, greenBtn, blueBtn, normalBtn, circleBtn;
    private Button btnRedo, btnUndo;    //重做，撤销按钮


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw);
        mPaint = new Paint();
        mPaint.setColor(0xFFFFFFF);
        mPaint.setStrokeWidth(3);

        mBrush = new IBrushNormal();

        mCanvas = findViewById(R.id.ac_draw_canvas);
        mCanvas.setOnTouchListener(new DrawTouchListener());

        btnRedo = findViewById(R.id.ac_draw_operate_redo_btn);
        btnRedo.setEnabled(false);
        btnRedo.setOnClickListener(this);

        btnUndo = findViewById(R.id.ac_draw_operate_undo_btn);
        btnUndo.setEnabled(false);
        btnUndo.setOnClickListener(this);

        redBtn = findViewById(R.id.ac_draw_color_red_btn);
        greenBtn = findViewById(R.id.ac_draw_color_green_btn);
        blueBtn = findViewById(R.id.ac_draw_color_blue_btn);
        normalBtn = findViewById(R.id.ac_draw_brush_normal_btn);
        circleBtn = findViewById(R.id.ac_draw_brush_circle_btn);
        redBtn.setOnClickListener(this);
        greenBtn.setOnClickListener(this);
        blueBtn.setOnClickListener(this);
        normalBtn.setOnClickListener(this);
        circleBtn.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ac_draw_color_red_btn:    //切换红色
                mPaint = new Paint();
                mPaint.setStrokeWidth(3);
                mPaint.setColor(0xFFFF0000);
                break;
            case R.id.ac_draw_color_green_btn:    //切换绿色
                mPaint = new Paint();
                mPaint.setStrokeWidth(3);
                mPaint.setColor(0xFF00FF00);
                break;
            case R.id.ac_draw_color_blue_btn:    //切换蓝色
                mPaint = new Paint();
                mPaint.setStrokeWidth(3);
                mPaint.setColor(0xFF0000FF);
                break;
            case R.id.ac_draw_operate_undo_btn:     //撤销操作
                mCanvas.undo();
                if (!mCanvas.canUndo()) {
                    btnUndo.setEnabled(false);
                }
                btnRedo.setEnabled(true);
                break;
            case R.id.ac_draw_operate_redo_btn:        //重做操作
                mCanvas.redo();
                if (!mCanvas.canRedo()) {
                    btnRedo.setEnabled(false);
                }
                btnUndo.setEnabled(true);
                break;
            case R.id.ac_draw_brush_circle_btn:        //切换园笔笔触
                mBrush = new IBrushCircle();
                break;
            case R.id.ac_draw_brush_normal_btn:        //切换正常（线）笔触
                mBrush = new IBrushNormal();
                break;
        }

    }

    private class DrawTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mPath = new IDrawPath();
                    mPath.paint = mPaint;
                    mPath.path = new Path();

                    mBrush.down(mPath.path, event.getX(), event.getY());
                    break;
                case MotionEvent.ACTION_MOVE:
                    mBrush.move(mPath.path, event.getX(), event.getY());
                    break;
                case MotionEvent.ACTION_UP:
                    mBrush.up(mPath.path, event.getX(), event.getY());

                    mCanvas.add(mPath);
                    mCanvas.isDrawing = true;

                    btnUndo.setEnabled(true);
                    btnRedo.setEnabled(false);
                    break;
            }
            return true;
        }
    }
}